const AUTH = require('../../../utils/auth')
import {
  acceptOrder
} from "../../../api/tradeOrderController";
import {
  getTradeOrderList
} from "../../../api/tradeOrderController";
import {
  wxJSAPIPayOrder,
  getPrepayIdByTradeOrderId
} from "../../../api/wxPayController";


Page({
  /**
   * 页面的初始数据
   */
  data: {
    statusType: ["全部", "待付款", "待接单", "配送中", "已完成"],
    currentType: 0, //展示模块
    status: '', //订单列表状态
    wxlogin: true,

    orderList: [
      [],
      [],
      [],
      [],
      []
    ], // 订单数据   二维数组缓存加载
    pageNo: [1, 1, 1, 1, 1], //第一页
    pageSize: 10, //十条
    isLastPage: [false, false, false, false, false], //是否是最后一页
    now: new Date().getTime(),
  },
  /**
   * 初始化展示模块
   */
  onLoad(options) {
    let {
      type
    } = options
    this.setData({
      currentType: type
    })
    this.getTradeOrderList()


    /**
     * 用来计算倒计时
     */
    this.data.myTime = setInterval(() => {
      this.setData({
        now: new Date().getTime()
      })
      console.log('倒计时');
    }, 1000);
  },
  /**
   * 倒计时
   */
  djs(leftTime){
    let self = this
    //时间差
    //定义变量 h,m,s保存倒计时的时间
    let h, m, s;
    if (leftTime >= 0 && leftTime<=1800000) {
      h = Math.floor(leftTime / 1000 / 60 / 60 % 24);
      h = h < 10 ? '0' + h : h
      m = Math.floor(leftTime / 1000 / 60 % 60);
      m = m < 10 ? '0' + m : m
      s = Math.floor(leftTime / 1000 % 60);
      s = s < 10 ? '0' + s : s
      this.setData({
        time: m + '分' + s + '秒'
      })
    } else {
      self.setData({
        time: '已超时'
      })
    }
  },
  /**
   * 倒计时
   * orderTime 下单时间
   */
  countTime(orderTime) {
    let leftTime = new Date().getTime() - orderTime
    setInterval(() => {
      leftTime -= 1000
      this.djs(leftTime)
    }, 1000);
  },
  /**
   * 配置订单status
   */
  getStatus() {
    let status = ''
    if (this.data.currentType == 0) {
      status = ''
    } else if (this.data.currentType == 1) {
      status = 10
    } else if (this.data.currentType == 2) {
      status = 20
    } else if (this.data.currentType == 3) {
      status = 30
    } else if (this.data.currentType == 4) {
      status = 40
    }
    this.setData({
      status: status
    })
  },
  /**
   * 查询列表  累加
   */
  async getTradeOrderList() {
    let self = this
    self.getStatus()
    let resolve = await getTradeOrderList({
      status: self.data.status,
      pageNo: self.data.pageNo[self.data.currentType],
      pageSize: self.data.pageSize,
      custId: wx.getStorageSync('custId')
    })
    let orderList = self.data.orderList[self.data.currentType]
    if (!self.data.isLastPage[self.data.currentType]) {
      self.setData({
        ['orderList[' + self.data.currentType + ']']: orderList.concat(resolve.data.list),
        ['isLastPage[' + self.data.currentType + ']']: resolve.data.isLastPage,
      })
    }
  },
  /**
   * 查询列表  单页赋值一页
   */
  async getTradeOrderOneList() {
    let self = this
    self.getStatus()
    let resolve = await getTradeOrderList({
      status: self.data.status,
      pageNo: self.data.pageNo[self.data.currentType],
      pageSize: self.data.pageSize,
      custId: wx.getStorageSync('custId')
    })
    if (!self.data.isLastPage[self.data.currentType]) {
      self.setData({
        ['orderList[' + self.data.currentType + ']']: resolve.data.list,
        ['isLastPage[' + self.data.currentType + ']']: resolve.data.isLastPage,
      })
    }
  },
  /**
   * 如果是最后一页
   * pageNo = 1 时  不用concat  直接 赋值list 
   */
  lastPageSetList() {
    let self = this
    if (!self.data.isLastPage[self.data.currentType]) {
      // if (self.data.pageNo[self.data.currentType]==1) {
      //   self.setData({
      //     ['orderList['+self.data.currentType+']']: resolve.data.list,
      //     ['isLastPage['+self.data.currentType+']']: resolve.data.isLastPage,
      //   })
      // }else{
      //   console.log(2);
      self.setData({
        ['orderList[' + self.data.currentType + ']']: orderList.concat(resolve.data.list),
        ['isLastPage[' + self.data.currentType + ']']: resolve.data.isLastPage,
      })
      // }
    }
  },
  /**
   * 设置页码第一页 刷新
   */
  noLastPageSetList() {
    let self = this
    let pageNo = [1, 1, 1, 1, 1]
    let isLastPage = [false, false, false, false, false]
    // let orderList = [[],[],[],[],[]]
    self.setData({
      pageNo: pageNo,
      isLastPage: isLastPage,
      // orderList: orderList
    })
    self.getTradeOrderOneList()
  },
  /**
   * 更改导航
   */
  statusTap: function (e) {
    const {
      index
    } = e.currentTarget.dataset
    this.setData({
      currentType: index,
      ['pageNo[' + this.data.currentType + ']']: this.data.pageNo[this.data.currentType]
    });

    // if (!this.data.isLastPage[this.data.currentType]) {
    //   this.getTradeOrderOneList()
    // }
    this.noLastPageSetList()
  },
  /**
   * 滚动到头
   */
  bindscrolltolower(e) {
    if (!this.data.isLastPage[this.data.currentType]) {
      this.data.pageNo[this.data.currentType]++
      this.setData({
        ['pageNo[' + this.data.currentType + ']']: this.data.pageNo[this.data.currentType]
      })
      this.getTradeOrderList()
    }
  },
  /**
   * 关闭弹窗
   */
  afterAuth() {
    this.setData({
      wxlogin: true
    })
  },
  /**
   * 支付
   */
  wxPay(e) {
    this.wxJSAPIPayOrder(e)
  },
  /**
   * 封装预下单方法
   */
  async wxJSAPIPayOrder(e) {
    let {
      wxpayouttradeno,
      id,
      amount
    } = e.currentTarget.dataset
    let self = this
    let resolve = await getPrepayIdByTradeOrderId({
      totalFee: amount * 100,
      productDesc: '云上漂支付测试',
      wxPayOutTradeNo: wxpayouttradeno,
      tradeOrderId: id,
      userLoginState: wx.getStorageSync('userLoginState')
    })
    setTimeout(() => {
      /**
       * 支付订单后 不管是不是第一页都需要重置第一页 刷新
       */
      self.noLastPageSetList()
    }, 1500);
  },
  onShow() {
    AUTH.checkHasLogined().then(isLogined => {
      console.log('登录状态', isLogined);
      if (isLogined) {
        this.setData({
          wxlogin: true
        })
      } else {
        this.setData({
          wxlogin: true
        })
      }
    })
  },
  onUnload(){
    clearInterval(this.data.myTime)
  },
  /**
   * 接单 0     转让  1       完成  2
   */
  async handlerClick(e) {
    let {
      id
    } = e.currentTarget.dataset
    let resolve = await acceptOrder({
      tradeOrderId: id
    })
    if (resolve.success) {
      this.noLastPageSetList()
    } else {
      wx.showToast({
        title: resolve.errorMessage,
        icon: 'error'
      })
    }
  },

})